package com.cbd.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbd.pojo.Renter;
import com.cbd.pojo.User;
import com.cbd.service.CompanyService;
import com.cbd.service.LandladyService;
import com.cbd.service.OperatorService;
import com.cbd.service.RenterService;
import com.cbd.service.UserService;

@Controller
public class UserController {
	/**
	 * @Fields count : 用于统计在线人数
	 */ 
	private  int count;
	/**
	 * @Fields flag : 用于判断用户是点击注销 或者是点击关闭窗口事件，true表示点击注销，false表示点击的关闭窗口
	 */ 
	private boolean flag=false;
	@Resource(name="userServiceImpl")
	private UserService userService;
	@Resource(name="landladyServiceImpl")
	private LandladyService landladyService;
	@Resource(name="renterServiceImpl")
	private RenterService renterService;
	@Resource(name="companyServiceImpl")
	private CompanyService companyService;
	@Resource(name="operatorServiceImpl")
	private OperatorService operatorService;
	
	@RequestMapping("findUserByName.do")
	public @ResponseBody User findUserByName(HttpServletRequest req){
		String name = req.getParameter("inputName");
		return userService.findUserByName(name);
	} 
	@RequestMapping("/cbd/Operator/findUserByName.do")
	public @ResponseBody User findUserByNames(HttpServletRequest req){
		String name = req.getParameter("name");
		return userService.findUserByName(name);
	} 
	@RequestMapping("login")
	public String login(@Valid User user,BindingResult br,HttpServletRequest req){
		
		if(br.hasErrors()){
			req.getSession().setAttribute("login", "0");
			return "redirect:login.jsp";
		}
		User myUser=userService.login(user);
		if(myUser==null){
			req.getSession().setAttribute("login", "0");
			return "redirect:login.jsp";
		}else{
			int role = myUser.getUserRole();
			String userName = myUser.getUserName();
			int id = myUser.getUserID();
		//	req.getSession().setAttribute("uName", userName);
			count++;
			req.getSession().getServletContext().setAttribute("count", count);
			if(role==0){
				req.getSession().setAttribute("myLandLady",landladyService.showlandLady(id));
				req.getSession().setAttribute("login", "1");
				return "redirect:cbd/landlady/landladyMain.jsp";
			}else if(role==1){
				req.getSession().setAttribute("myRenter",renterService.findRenterByUserID(id));
				req.getSession().setAttribute("login", "2");
				return "redirect:cbd/renter/renterMain.jsp";
			}else if(role==2){
				req.getSession().setAttribute("myCompany",companyService.findCompanyByUserId(id));
				req.getSession().setAttribute("login", "3");
				return "redirect:cbd/company/companyMain.jsp";
			}else if(role==3){
				req.getSession().setAttribute("myOperator", operatorService.findOperatorBuUserId(id));
				req.getSession().setAttribute("login", "4");
				return "redirect:cbd/Operator/operatorMain.jsp";
			}else{
				return "redirect:login.jsp";
			}
			
		}
	}
	@RequestMapping("/cbd/Operator/getCount")
	public void getCount(PrintWriter pw){
		pw.print(count);
	}
	@RequestMapping("handCount")
	public void handCount(HttpServletRequest req){
		if(!flag){
			if(req.getSession().getAttribute("myOperator")==null){
				count = (int) req.getSession().getServletContext().getAttribute("count");
			}
			if(req.getSession().getAttribute("myCompany")==null){
				count = (int) req.getSession().getServletContext().getAttribute("count");
			}
			if(req.getSession().getAttribute("myLandLady")==null){
				count = (int) req.getSession().getServletContext().getAttribute("count");
			}if(req.getSession().getAttribute("myRenter")==null){
				count = (int) req.getSession().getServletContext().getAttribute("count");
			}	
			count--;
			req.getSession().getServletContext().setAttribute("count", count);
		}
	
	}
	@RequestMapping("/cbd/Operator/loginout")
	public String loginOut(HttpServletRequest req){
		String role = req.getParameter("role");
		//req.getSession().removeAttribute("uName");
		req.getSession().removeAttribute("login");
		switch (role) {
		case "myOperator":
			req.getSession().removeAttribute("myOperator");
			break;
		case "myCompany":
			req.getSession().removeAttribute("myCompany");
			break;
		case "myLandLady":
			req.getSession().removeAttribute("myLandLady");
			break;
		case "myRenter":
			req.getSession().removeAttribute("myRenter");
			break;
		default:
			break;
		}
		flag=true;
		count = (int) req.getSession().getServletContext().getAttribute("count");
		count--;
		req.getSession().getServletContext().setAttribute("count", count);
		return "redirect:../../login.jsp";
		
	}
}
